package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {

    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{}",file);
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString() + extension;


            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.info("文件上传失败:{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);//"文件上传失败"
    };

    @GetMapping("/test")
    @ApiOperation("文件上传")
    public Result<Data> test(MultipartFile file){
        ArrayList<String> strings = new ArrayList<>();
        strings.add("123");
        strings.add("234");
        strings.add("345");
        strings.add("456");
        HashMap<String, String> maps = new HashMap<>();
        maps.put("123","234");
        maps.put("234","345");
        maps.put("456","567");
        maps.put("678","789");

        Data data = new Data("12",maps,strings);
        data.setId("12");
//        data.getHashMap().put("12","58");
//        data.getHashMap().put("36","69");
//        data.getHashMap().put("58","5u");
//        data.getHashMap().put("75","67");
//        data.getList().add("123");
//        data.getList().add("234");
//        data.getList().add("345");
//        data.getList().add("456");
        return Result.success(data);
    };

}
